var QRCode = require('../../../utils/weapp-qrcode.js');
var qrcode;
const app = getApp();
var util = require('../../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:applet_park_api_url_imgUrl,
    orderNo: '',
    restaurantName: '',
    status: '',
    type: '',//1店内就餐 2预定取餐
    pickUpTime: '',
    dataList: [],
    showModal: false,
    meetingDetail:'',
    conferenceRoomId:'',
    conferenceDeviceList:[],
    conferenceOrder:'',
    appointmentDate:'',
    conferenceParticipantVOList:[],//参会人员
    conferenceOrderDetailVOList:[],//增值服务
    totalAmount:'',
    id:'',
    openId: wx.getStorageSync("OPENID"),
    token: wx.getStorageSync("TOKEN"),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      orderNo: options.orderNo,
      conferenceRoomId: options.conferenceRoomId
    })
    that.getqrcode();
    that.getOrderInfo(options.orderNo);
  },
  //获取订单详情
  getOrderInfo(orderNo) {
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
    var userId = wx.getStorageSync("id");
   //var url = '/api/conferenceOrders/'+2;
    var url = '/api/conferenceOrders/' + that.data.conferenceRoomId;
      requestDataparams(url, {}, 'GET').then(res => {
        if (res.data.code == 0) {
          let items = [];
          var lists = res.data.data;
          let url = applet_park_api_url_imgUrl;
            if (lists.conferenceRoomVO.images) {
              lists.conferenceRoomVO.images = url +'/'+ lists.conferenceRoomVO.images.split(',')[0];
            }
          if(lists.appointmentEndTime){//结束时间
            lists.appointmentEndTime = lists.appointmentEndTime.substring(11,16);
          }
          var openingDate = '';
          if(lists.appointmentStartTime){//开始时间
            openingDate = lists.appointmentStartTime.substring(0,10);
            lists.appointmentStartTime = lists.appointmentStartTime.substring(11,16);
          }
          for(var i=0;i<lists.conferenceOrderDetailVOList.length;i++){//增值服务图片
             if(lists.conferenceOrderDetailVOList[i].conferenceGoodVO.images){
              lists.conferenceOrderDetailVOList[i].conferenceGoodVO.images =  applet_park_api_url_imgUrl +'/'+ lists.conferenceOrderDetailVOList[i].conferenceGoodVO.images.split(',')[0];
             }
          }

          that.setData({
            meetingDetail: lists,
            conferenceDeviceList: lists.conferenceRoomVO.conferenceDeviceList,
            conferenceOrder: lists.conferenceOrder,//会议内容
            appointmentDate: openingDate,
            conferenceParticipantVOList:lists.conferenceParticipantVOList,
            conferenceOrderDetailVOList:lists.conferenceOrderDetailVOList,
            totalAmount: lists.totalAmount,
            id: lists.id
          })
          if (that.data.data) {
            that.setData({
              title: '暂无更多数据',
            })
          } else {
            that.setData({
              title: ''
            })
          }
  
        }
        wx.hideLoading();
      }).catch(err => {
        wx.showToast({
          title: '网络异常，请稍后重试',
        })
        wx.hideLoading();
      });
    
  },
  
  //处理门店头像和食品头像
  getPic(e) {
    var imgUrl = e.split(',')[0];
    imgUrl = applet_park_api_url_imgUrl + imgUrl;
    return imgUrl;
  },
  getqrcode() {
    var that = this;
    qrcode = new QRCode('canvas', {
      text: that.data.orderNo,
      width: 150,
      height: 150,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  goPay(e){//直接支付
     var that = this;
     var totalAmount = e.currentTarget.dataset.totalamount;
     var totlaPrice = totalAmount + '00';
     var orderId = e.currentTarget.dataset.id;
     wx.request({
      url: applet_park_api_url + '/api/wechatPay/conferenceOrderListPay',
      method: 'POST',
      header: {
        Authorization:that.data.token,
        'Content-Type':"application/x-www-form-urlencoded",
      },
      data: {
        totalAmount: 1,//that.data.totalAmount
        orderId: that.data.id,
        openid: that.data.openId
      },
      success(res2) {
        if (res2.data.code == 0) {
          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function() {//刷新列表
                that.getOrderInfo(that.data.orderNo);
              }, 500);
            },
            fail(res3) {
              
            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  enlargeQrcode() {
    var that = this;
    that.setData({
      showModal: true
    })
    qrcode = new QRCode('canvas1', {
      text: that.data.orderNo,
      width: 250,
      height: 250,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  hideModal() {
    var that = this;
    that.setData({
      showModal: false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})